﻿using System;

namespace Edu.Utilities
{
    public static class TimeSpanExtensions
    {
        public const double AvgDaysInAYear = 365.2425d;
        public const double AvgDaysInAMonth = 30.436875d;

        public static int GetYears(this TimeSpan timespan)
        {
            return (int)(timespan.TotalDays / AvgDaysInAYear);
        }

        public static double GetTotalYears(this TimeSpan timespan)
        {
            return timespan.TotalDays / AvgDaysInAYear;
        }

        public static int GetMonths(this TimeSpan timespan)
        {
            return (int)(timespan.TotalDays / AvgDaysInAMonth);
        }

        public static double GetTotalMonths(this TimeSpan timespan)
        {
            return timespan.TotalDays / AvgDaysInAMonth;
        }

        public static int GetWeeks(this TimeSpan timespan)
        {
            return (int)(timespan.TotalDays / 7d);
        }

        public static double GetTotalWeeks(this TimeSpan timespan)
        {
            return timespan.TotalDays / 7d;
        }

        public static double GetDays(this TimeSpan timespan)
        {
            return timespan.TotalDays;
        }

        public static double GetMicroseconds(this TimeSpan span)
        {
            return span.Ticks / 10d;
        }

        public static double GetNanoseconds(this TimeSpan span)
        {
            return span.Ticks / 100d;
        }

        public static TimeSpan Round(this TimeSpan time, TimeSpan roundingInterval, MidpointRounding roundingType = MidpointRounding.ToEven)
        {
            return new TimeSpan(Convert.ToInt64(Math.Round(time.Ticks / (double)roundingInterval.Ticks, roundingType)) * roundingInterval.Ticks);
        }

        public static TimeSpan Floor(this TimeSpan time, TimeSpan roundingInterval)
        {
            return new TimeSpan(Convert.ToInt64(Math.Floor(time.Ticks / (double)roundingInterval.Ticks)) * roundingInterval.Ticks);
        }

        public static TimeSpan Ceiling(this TimeSpan time, TimeSpan roundingInterval)
        {
            return new TimeSpan(Convert.ToInt64(Math.Ceiling(time.Ticks / (double)roundingInterval.Ticks)) * roundingInterval.Ticks);
        }



    }

}
